package com.shark.uh.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.shark.uh.common.ApiResponse;
import com.shark.uh.model.dto.auth.LoginCheckDTO;
import com.shark.uh.model.dto.auth.LoginCheckResultDTO;
import com.shark.uh.model.dto.auth.LoginDTO;
import com.shark.uh.model.dto.auth.RegisterDTO;
import com.shark.uh.service.AuthService;
import com.shark.uh.service.RedisService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.websocket.server.PathParam;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;

@Controller
public class AuthController {

    @Resource
    private AuthService authService;

    @Resource
    private RedisService redisService;


    @PostMapping("api/auth/register")
    @ResponseBody
    public ApiResponse<Object> register(@Valid @RequestBody RegisterDTO registerDTO) {
        authService.register(registerDTO);
        return ApiResponse.success("注册成功");
    }


    /**
     * 激活账号页面
     *
     * @param model model
     * @param token 唯一tiken
     * @return html
     */
    @GetMapping("view/auth/active-account/{token}")
    public String toActiveAccountPage(Model model, @PathVariable String token) {
        String email = redisService.getString(token);

        model.addAttribute("token", token);
        model.addAttribute("email", email);
        return "account-active";
    }

    /**
     * 激活账号逻辑
     *
     * @param model model
     * @param token 唯一token
     * @return 激活结果页
     */
    @GetMapping("view/auth/active")
    public String activeAccount(Model model, @PathParam("token") String token) {
        String email = redisService.getString(token);
        if (email == null) {
            model.addAttribute("result", "验证码已过期");
            return "active-result";
        }

        int count = authService.activeAccount(token);
        if (count < 1) {
            model.addAttribute("result", "激活失败");
        }
        model.addAttribute("result", "激活成功");
        return "active-result";
    }


    /**
     * 用户登录
     *
     * @param loginDTO 登录请求体
     * @return ApiResponse
     */
    @ResponseBody
    @PostMapping("api/auth/login")
    public ApiResponse<Object> login(@Valid @RequestBody LoginDTO loginDTO) throws UnsupportedEncodingException {
        Object data = authService.login(loginDTO);
        return ApiResponse.success(data);
    }

    @ResponseBody
    @PostMapping("api/login-check")
    public ApiResponse<Object> loginCheck(@RequestBody LoginCheckDTO loginCheckDTO, HttpServletRequest request) {
        loginCheckDTO.setRequest(request);
        LoginCheckResultDTO loginCheckResultDTO = authService.checkLogin(loginCheckDTO);
        return ApiResponse.success(loginCheckResultDTO);
    }

    @ResponseBody
    @GetMapping("api/auth/token/valid")
    public ApiResponse<Object> isValidToken(HttpServletRequest request) {
        String sharkToken = request.getHeader("Authorization");
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("isValidToken", StpUtil.getLoginIdByToken(sharkToken) == null);
        return ApiResponse.success(hashMap);
    }

}
